﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AteneoLibriBusinessLogic.Entities;
using AteneoLibriBusinessLogic.MyException;

namespace AteneoLibriBusinessLogic.DAL
{
    public class SaleFacade
    {

        AteneoLibriEntityFramework.AteneoLibriEntities db = new AteneoLibriEntityFramework.AteneoLibriEntities();

        public List<Sale> getSales()
        {
            List<Sale> sales = new List<Sale>();
            List<AteneoLibriEntityFramework.Sale> salesEF = db.Sales.ToList();

            foreach (var item in salesEF)
            {
                var sale = TOA.SaleAssembler.Assembler(item);
                sales.Add(sale);
            }

            return sales;
        }

        /// <summary>
        /// Restituisce il numero di vendite legate a quel dato libro.
        /// Appena concludo una vendita, tutti gli altri acquirenti devono essere notificati di questo avvenimento
        /// </summary>
        /// <param name="bookId"></param>
        /// <returns></returns>
        public List<Sale> getSalesByBookId(Guid bookId)
        {
            List<Sale> salesOnGoing = new List<Sale>();
            List<AteneoLibriEntityFramework.Sale> sales = db.Sales.Where(x => x.BookId == bookId).ToList();
            foreach (var item in sales)
            {
                var sale = TOA.SaleAssembler.Assembler(item);
                salesOnGoing.Add(sale);
            }

            return salesOnGoing;
        }

        public List<Sale> getSalesByUserId(Guid userId)
        {
            List<Sale> mySales = new List<Sale>();
            List<AteneoLibriEntityFramework.Sale> sales = db.Sales.Where(x => x.UserId == userId).ToList();
            foreach (var item in sales)
            {
                var sale = TOA.SaleAssembler.Assembler(item);
                mySales.Add(sale);
            }
            return mySales;
        }

        public bool insertSale(Sale sale)
        {
            bool result = false;
            try
            {
                AteneoLibriEntityFramework.Sale saleEF = TOA.SaleAssembler.DeAssembler(sale);
                db.Sales.AddObject(saleEF);
                db.SaveChanges();

                result = true;
            }
            catch (Exception ex)
            {
                throw new DALException(ex.Message, ex);
            }

            return result;
        }


        public bool existPreviousRequest(Guid userId, Guid bookId)
        {
            bool result = false;
            try
            {
                result = this.getSales().Where(x => x.UserIdPurchaser == userId && x.BookId == bookId).Count() > 0;
            }
            catch (Exception ex)
            {
                throw new DALException(ex.Message, ex);
            }

            return result;
        }

        public bool updateSaleStatus(Sale sale, Guid saleStatusId)
        {
            bool result = false;
            try
            {
                var s = db.Sales.Where(x => x.IdSale == sale.IdSale).FirstOrDefault();
                if (s != null)
                {
                    s.SaleStatusId = saleStatusId;
                }

                db.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new DALException(ex.Message, ex);
            }

            return result;
        }

        public bool setEndDate(Sale sale, DateTime endDate)
        {
            bool result = false;
            try
            {
                var s = db.Sales.Where(x => x.IdSale == sale.IdSale).FirstOrDefault();
                if (s != null)
                {
                    s.EndDate = endDate;
                }

                db.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new DALException(ex.Message, ex);
            }
            return result;
        }

        public bool updateSale(Sale sale)
        {
            bool result = false;
            try
            {
                AteneoLibriEntityFramework.Sale saleEF = db.Sales.Where(x => x.IdSale == sale.IdSale).FirstOrDefault();
                //saleEF = TOA.SaleAssembler.DeAssembler(sale);
                TOA.SaleAssembler.WriteToEntity(saleEF, sale);

                db.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);
                result = true;
            }
            catch (Exception ex)
            {
                throw new DALException(ex.Message, ex);
            }            

            return result;
        }

        public bool deleteSales(Guid userId)
        {
            bool result = false;
            try
            {
                List<AteneoLibriEntityFramework.Book> books = db.Books.Where(x => x.UserId == userId).ToList();
                List<AteneoLibriEntityFramework.Sale> sales = db.Sales.Where(x => x.UserId == userId).ToList();

                foreach (var item in books)
                {
                    sales.Add(db.Sales.Where(x => x.BookId == item.IdBook).First());
                }

                foreach (var item in sales)
                {
                    db.Sales.DeleteObject(item);
                }

                db.SaveChanges();
                result = true;
            }
            catch (Exception ex)
            {
                throw new DALException(ex.Message, ex);
            }

            return result;
        }
    }
}
